package whf.file.web;

import java.util.Map;

import org.apache.struts.upload.FormFile;

import whf.file.Constants;
import whf.file.entity.File;
import whf.file.entity.Type;
import whf.file.util.DiskFileManager;
import whf.file.util.FileManager;
import whf.framework.entity.AbstractEntity;
import whf.framework.util.Utils;
import whf.framework.util.images.ScaleProperty;
import whf.framework.web.struts.WebData;
import whf.framework.web.struts.WebDispatchAction;
import whf.framework.web.struts.WebForward;

/**
 * @author wanghaifeng
 * @create Nov 16, 2006 10:16:50 PM
 * 
 */
public class TypeAction extends WebDispatchAction {
	private static FileManager fileManager = DiskFileManager.getFileManager();
	
	@Override
	public WebForward create(AbstractEntity baseForm, WebData data) throws Exception {
		Type fileType = (Type) baseForm;
		Map<String, ScaleProperty> props = Utils.newHashMap();
		FormFile formFile = null;
		if (fileType.getLargeIconFile() != null && fileType.getLargeIconFile().getFileSize() > 0) {
			formFile = fileType.getLargeIconFile();
		}
		if (fileType.getMiddleIconFile() != null && fileType.getMiddleIconFile().getFileSize() > 0) {
			formFile = fileType.getMiddleIconFile();
		}
		if (fileType.getSmallIconFile() != null && fileType.getSmallIconFile().getFileSize() > 0) {
			formFile = fileType.getSmallIconFile();
		}
		if(formFile != null){
			props.put(Constants.ICON_TYPE_LARGE, Constants.LARGE_SCALE_PROPERTIES);
			props.put(Constants.ICON_TYPE_MIDDLE, Constants.MIDDLE_SCALE_PROPERTIES);
			props.put(Constants.ICON_TYPE_SMALL, Constants.SMALL_SCALE_PROPERTIES);
			String fileName = fileManager.saveImage(formFile.getInputStream(), formFile.getFileName(), props);
			fileType.setLargeIcon(whf.file.entity.File.convertIconFileName(fileName, Constants.ICON_TYPE_LARGE));
			fileType.setMiddleIcon(whf.file.entity.File.convertIconFileName(fileName, Constants.ICON_TYPE_MIDDLE));
			fileType.setSmallIcon(whf.file.entity.File.convertIconFileName(fileName, Constants.ICON_TYPE_SMALL));
		}
		return super.create(baseForm, data);
	}
	
	@Override
	public WebForward delete(AbstractEntity baseForm, WebData data) throws Exception {
		Type fileType = (Type) baseForm;
		fileManager.delete(fileType.getLargeIcon());
		fileManager.delete(fileType.getMiddleIcon());
		fileManager.delete(fileType.getSmallIcon());
		return super.delete(baseForm, data);
	}

	@Override
	public WebForward update(AbstractEntity baseForm, WebData data) throws Exception {
		Type fileType = (Type) baseForm;
		Map<String, ScaleProperty> props = Utils.newHashMap();
		FormFile formFile = null;
		if (fileType.getLargeIconFile() != null && fileType.getLargeIconFile().getFileSize() > 0) {
			formFile = fileType.getLargeIconFile();
		}
		if (fileType.getMiddleIconFile() != null && fileType.getMiddleIconFile().getFileSize() > 0) {
			formFile = fileType.getMiddleIconFile();
		}
		if (fileType.getSmallIconFile() != null && fileType.getSmallIconFile().getFileSize() > 0) {
			formFile = fileType.getSmallIconFile();
		}
		if(formFile != null){
			props.put(Constants.ICON_TYPE_LARGE, Constants.LARGE_SCALE_PROPERTIES);
			props.put(Constants.ICON_TYPE_MIDDLE, Constants.MIDDLE_SCALE_PROPERTIES);
			props.put(Constants.ICON_TYPE_SMALL, Constants.SMALL_SCALE_PROPERTIES);
			String fileName = fileManager.saveImage(formFile.getInputStream(), formFile.getFileName(), props);
			fileType.setLargeIcon(whf.file.entity.File.convertIconFileName(fileName, Constants.ICON_TYPE_LARGE));
			fileType.setMiddleIcon(whf.file.entity.File.convertIconFileName(fileName, Constants.ICON_TYPE_MIDDLE));
			fileType.setSmallIcon(whf.file.entity.File.convertIconFileName(fileName, Constants.ICON_TYPE_SMALL));
			data.putData("largeIcon", File.convertIconFileName(fileName, Constants.ICON_TYPE_LARGE));
			data.putData("middleIcon", File.convertIconFileName(fileName, Constants.ICON_TYPE_MIDDLE));
			data.putData("smallIcon", File.convertIconFileName(fileName, Constants.ICON_TYPE_SMALL));
		}
		return super.update(fileType, data);
	}

}